package Login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //这里先写死用户名为zhangsan，密码为123才能登录成功,否则登录失败
        if(!username.equals("zhangsan") && !password.equals("123")) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("登录失败");
        }

        HttpSession session = req.getSession(true);
        session.setAttribute("username",username);
        session.setAttribute("loginTime",System.currentTimeMillis());
        //此时就相当于登录成功，所以重定向到新的页面
        resp.sendRedirect("index");
    }
}
